<?php

namespace app\helper\ChannelSdk;

use app\atrait\Single;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class UMark
{

    use Single;

    protected string $appKey;
    protected string $orderKey;
    protected string $notifyKey;

    public function __construct()
    {
        $this->appKey = '7a334fe381484dd6bf4bbf20a3b199e2';
        $this->orderKey = '80bf0fc5dde94be2b3abe17bf0d825bb';
        $this->notifyKey = 'd1d29b7a57324aec9a7b472949c15592';
    }


    public function loginAuth($platform, $uid, $token): array
    {

        $url = 'http://user-api.6075.com/game/verify/token';

        $appId = '97ac9e57b26b5a85f9da470f6bea061e';
        $serverKey = '435edafa3574e4ea5d0bc974b3b8455e';

        $param = [
            'appId' => '97ac9e57b26b5a85f9da470f6bea061e',
            'token' => $token,
            'sign' => md5($appId . $token . $serverKey)
        ];

        $http = new Client();
        try {
            $res = $http->request('POST', $url, [
                'json' => $param,
            ]);
        } catch (GuzzleException $e) {
            return [
                'state' => false,
                'message' => $e->getMessage(),
            ];
        }

        $result = json_decode($res->getBody(), true);

        return [
            'state' => $result['code'] === 1,
            'message' => $result['code'] === 1 ? '' : $result['msg'],
            'data' => $result['code'] === 1 ? [
                'uid' => $result['data']['userId'],
            ] : [],
        ];

    }

}